package com.bootdo.common.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import com.bootdo.common.utils.NumberUtil;

import org.eclipse.jetty.util.log.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 
 * @author 路由类
 *
 */
public class MyAbstractRoutingDataSource extends AbstractRoutingDataSource{
	 //@Value("${mysql.datasource.num}")
	 private int num = 1;
 
	 private final Logger log = LoggerFactory.getLogger(this.getClass());
	 
		 @Override
	protected Object determineCurrentLookupKey() {
		 String typeKey = DbContextHolder.getDbType();
		 if (typeKey == null || typeKey == DbContextHolder.WRITE) {
			 log.info("使用了写库");
			 return DbContextHolder.WRITE;
		 }
		 //使用随机数决定使用哪个读库
		 int sum = NumberUtil.getRandom(1, num);
		 log.info("使用了读库{}",sum);
		 return DbContextHolder.READ+sum;
	 }

	 
	 
	 
	 
	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}
	 
	 
	 
}
